package de.karlNet.sambamanager.view.beans;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope; import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Controller;

import de.karlNet.sambamanager.ldapController.commands.RemoveAllSubGroupsFromGroup;
import de.karlNet.sambamanager.ldapController.commands.RemoveGroupFromGroup;
import de.karlNet.sambamanager.ldapController.commands.RemoveUserFromGroup;
import de.karlNet.sambamanager.ldapModel.ITypeCheckerCallback;
import de.karlNet.sambamanager.ldapModel.LdapGroup;
import de.karlNet.sambamanager.ldapModel.LdapObject;
import de.karlNet.sambamanager.ldapModel.LdapUser;
import de.karlNet.sambamanager.view.beans.trees.LdapGroupBean;
import de.karlNet.sambamanager.view.beans.trees.SelectedNodeContainer;

@Controller
@Scope(value="session", proxyMode=ScopedProxyMode.TARGET_CLASS)
public class RemoveGroupFromGroupBean {
	@Autowired
	private SelectedNodeContainer selectedNodeContainer;
	@Autowired
	private RemoveGroupFromGroup removeGroupFromGroup;
	@Autowired
	private LdapGroupBean ldapGroupBean;
	@Autowired
	private RemoveAllSubGroupsFromGroup removeAllSubGroupsFromGroup;

	public void removeGroupMemberOfRelation() {
		final LdapGroup selectedObject = (LdapGroup) this.selectedNodeContainer.selectedNode
				.getLdapObject();
		final LdapObject parent = ((ManualLeafHandlingTreeNode)(this.selectedNodeContainer.selectedNode.getParent()))
				.getLdapObject();
		parent.isType(new ITypeCheckerCallback() {
			@Override
			public void isGroup(LdapGroup group) {
				removeGroupFromGroup.removeGroupFromGroup(selectedObject,
						group);
				selectedNodeContainer.selectedNode.setParent(null);
			}

			@Override
			public void isUser(LdapUser ldapUser) {
				new RemoveUserFromGroup().removeUserFromGroupItself(
						selectedObject, ldapUser);
				selectedNodeContainer.selectedNode.setParent(null);
			}
		});
	}

	public void removeGroupFromGroup() {
		LdapGroup nestedGroup = (LdapGroup) this.selectedNodeContainer.selectedNode
				.getLdapObject();
		LdapGroup parentGroup = (LdapGroup) ((ManualLeafHandlingTreeNode) this.selectedNodeContainer.selectedNode
				.getParent()).getLdapObject();
		this.removeGroupFromGroup
				.removeGroupFromGroup(parentGroup, nestedGroup);
		this.ldapGroupBean.reset();
		this.selectedNodeContainer.selectedNode.setParent(null);
	}

	public void removeAllSubGroupsFromGroups() {
		LdapGroup parentGroup = (LdapGroup) ((ManualLeafHandlingTreeNode) this.selectedNodeContainer.selectedNode)
				.getLdapObject();
		this.removeAllSubGroupsFromGroup
				.removeAllSubGroupsFromGroup(parentGroup);
		this.ldapGroupBean.reset();
	}
}
